<template>
  <div>
    <el-row>
      <el-col :span="8" v-for="item in tableData" :key="item._id">
        <el-card
          :body-style="{ padding: '20px'}"
          style="margin: 0 0 18px 18px; "
          :class="`${tempId}` === `${item._id}` ? 'borderStyle' : ''"
        >
          <i
            v-if="!tempType"
            style="float: right;"
            class="el-icon-delete-solid"
            @click="delPackage(item)"
          ></i>
          <div style="padding: 14px;" @click="changeSelect(item)">
            <span v-if="!!tempType">{{item.desc}}</span>
            <span  v-else class="foodName">{{item.models}}
            <p class="foodDes">描述：{{item.desc}}</p>
            </span>
            <div class="bottom clearfix">
              <p class="time">配送时间:&nbsp&nbsp{{item.startTime}}~{{item.endTime}}</p>
              <p class="money">￥{{ item.price }}</p>
              <el-button type="text" class="button" @click="packageDialog(item)">查看套餐</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 分页 -->
    <el-row style="text-align: right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[9, 18, 36, 72]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-row>
    <el-dialog
      title="套餐详情"
      :visible.sync="packageDialogVisible"
      width="40%"
      :before-close="dialogClose"
    >
      <el-form ref="form" :model="packageForm" label-width="130px">
        <el-form-item label="套餐类型"
        placeholder="请选择套餐类型" style="width: 100%;"
        >
          <el-input :readonly="!editFlag" v-model="packageForm.models"></el-input>
        </el-form-item>
        <el-form-item label="套餐说明">
          <el-input :readonly="!editFlag"
            :rows="2"
            placeholder="请输入内容"
           type="textarea"
          v-model="packageForm.desc"></el-input>
        </el-form-item>
        <el-form-item label="开始配送时间"
        >
          <el-time-select :readonly="!editFlag"
           placeholder="开始时间"
          v-model="packageForm.startTime"
          :picker-options="{
           start: '08:30',
           step: '00:15',
           end: '18:30'
           }">
           </el-time-select>
        </el-form-item>
        <el-form-item label="结束配送时间">
          <el-time-select :readonly="!editFlag"
           placeholder="结束时间"
           v-model="packageForm.endTime"
          :picker-options="{
           start: '08:30',
           step: '00:15',
           end: '18:30',
           minTime:startTime
           }"></el-time-select>
        </el-form-item>
        <el-form-item label="套餐价格">
          <el-input :readonly="!editFlag" v-model="packageForm.price"></el-input>
        </el-form-item>
        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div v-if="!editFlag">
          <el-button v-if="routerName === 'rentalpackage'" type="info" @click="editClick">修 改</el-button>
          <el-button type="primary" @click="packageDialogVisible = false">确 定</el-button>
        </div>
        <div v-else>
          <el-button type="primary" @click="save">保 存</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      packageForm: {},
      total: 0,
      pageSize: 9,
      pageNum: 1,
      tempId: 1,
      tableData: [],
      packageDialogVisible: false,
      packageForm: {},
      editFlag: false
    }
  },
  props: {
    flag: {
      type: Boolean
    },
    tempType: {
      type: String
    }
  },
  mounted () {
    this.getList()
  },
  computed: {
    routerName () {
      return this.$route.name
    }
  },
  watch: {
    flag () {
      this.getList()
    },
    tempType: {
      handler (v) {
        this.getList(v)
      },
      immediate: true
      // deep: true
    }
  },
  methods: {
    getList(v) {
      this.packageForm.pageSize = this.pageSize
      this.packageForm.pageNum = this.pageNum
      if (v) {
        this.packageForm.models = v
      }
      this.$api
        .get('/api/package/list', { params: this.packageForm })
        .then(res => {
          this.tableData = res.data.data.records
          this.total = res.data.data.total
        })
        .catch(err => {
          console.log(err)
          this.$message.error('获取列表失败')
        })
    },
    editClick () {
      this.editFlag = true
    },
    save () {
      console.log(this.packageForm)
      this.$api
        .post(`/api/package/edit/${this.packageForm._id}`, this.packageForm)
        .then(res => {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.editFlag = false
          this.getList()
        })
    },
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.pageNum = 1
      this.getList()
    },
    changeSelect (item) {
      this.tempId = item._id
      this.$emit('packageChange', item)
    },
    handleCurrentChange (pageNum) {
      this.pageNum = pageNum
      this.getList()
    },
    delPackage (item) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$api
            .delete(`/api/package/delete/${item._id}`)
            .then(res => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.pageNum = 1
              this.getList()
            })
            .catch(err => {
              this.$message({
                type: 'error',
                message: '删除失败'
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    dialogClose () {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.packageDialogVisible = false
        })
        .catch(_ => {})
    },
    packageDialog (item) {
      this.packageForm = item
      this.packageDialogVisible = true
    }
  }
}
</script>

<style scoped>
.time {
  font-size: 13px;
  color: #000;
  font-weight:600;
}
.money{
  font-size:12px;
  color:orange;
}

.bottom {
  margin-top: 12px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
.borderStyle {
  border: 1px solid gray;
}
.foodName{
font-size:16px;
font-weight:500;
color:orange;
}
.foodDes{
  font-size:12px;
  color:#999;
  font-weight:600;
}
</style>
